<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: SiteNews.php 320 2008-11-23 07:18:37Z steen.rabol@gmail.com $
 *
 */

Prado::using('Application.Common.XMimeMail');

class SiteNews extends PortalAdminPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);

		$this->setAdminPageTitle('Write Newsletter');

		if(TPropertyValue::ensureInteger($this->Request['MailSend']) == 0)
		{
			$this->HtmlContent->setWidth("100%");
		}
		else
		{
			$this->pnlSiteNews->Visible	= false;
			$this->pnlSiteNews->Enabled	= false;

			$this->pnlSiteNewsSendMail->Visible	= true;
			$this->pnlSiteNewsSendMail->Enabled	= true;
		}
	}

	public function sendNews($sender, $param)
	{
		if($this->IsValid)
		{
			$users = $this->DataAccess->createCommand("select * from tblusers where sitenews = 1")->query();
			if($users && count($users) > 0)
			{
				$xMail = new XMimeMail();
				$xMail->setCharset('iso-8859-1');
 				$xMail->setHtmlBody($this->HtmlContent->Text,'');
	 			$params['html_encoding'] = 'quoted-printable';
 				$params['text_encoding'] = 'quoted-printable';
 				$params['text_wrap'] = 998;
 				$xMail->prepare($params);

 				$senderrors	= array();
	 			foreach($users as $user)
	 			{
	 				try
	 				{
	 					$xMail->send($user['full_name'], $user['email'], "Prado portal", $this->Application->Parameters['AdminEmail'], $this->Subject->Text);
	 				}
	 				catch(Exception $e)
	 				{
	 					$senderrors[] = $e->getMessage();
	 				}
				}

				if(count($senderrors) > 0)
				{
					foreach($senderrors as $serror)
					{
						Prado::log($serror,TLogger::ALERT);
					}
				}
				else
				{
					$this->Response->redirect($this->Service->constructUrl('Admin.SiteNews', array("MailSend" => 1), false));
				}
			}
		}
	}
}
?>